package com.lkwlw.tplms.repertory.action;

import java.util.List;
import java.util.Map;

import com.lkwlw.tplms.order.dao.mapper.OrderinfoMapper;
import com.lkwlw.tplms.order.dao.model.Orderinfo;
import com.lkwlw.tplms.repertory.dao.mapper.InventoryinfoMapper;
import com.lkwlw.tplms.repertory.dao.mapper.WarehouseinfoMapper;
import com.lkwlw.tplms.repertory.dao.model.Inventoryinfo;
import com.lkwlw.tplms.repertory.dao.model.InventoryinfoExample;
import com.lkwlw.tplms.repertory.dao.model.Warehouseinfo;
import com.lkwlw.tplms.repertory.dao.model.WarehouseinfoExample;
import com.lkwlw.tplms.repertory.dao.model.WarehouseinfoExample.Criteria;
import com.lkwlw.tplms.system.dao.model.Admininfo;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class QueryRepertoryAction extends ActionSupport {
	private static final long serialVersionUID = 342058255698995274L;
	private InventoryinfoMapper inventoryinfoMapper;
	private WarehouseinfoMapper warehouseinfoMapper;
	private OrderinfoMapper orderInfoDAO;
	private Inventoryinfo inventoryinfo;	
	private Warehouseinfo warehouseinfo;
	private Orderinfo order;	
	private List<Warehouseinfo> warehouseList;
	private List<Map<String,Object>> stockInOrderList;	
	private String orderid;
	private String errorMessage;	

	public String execute() throws Exception {
		ActionContext context = ActionContext.getContext();
		Map session = context.getSession();
		Admininfo admin = (Admininfo) session.get("admin");
		//errorMessage = "";
		if (null != admin) {
			WarehouseinfoExample example = new WarehouseinfoExample();
			Criteria criteria = example.createCriteria();
			criteria.andWhkeeperidEqualTo(admin.getId());
			warehouseList = warehouseinfoMapper.selectByExample(example);
			if (null != warehouseList && warehouseList.size() > 0) {
				warehouseinfo = warehouseList.get(0);
				if (!session.containsKey("warehouse")) {
					session.put("warehouse", warehouseList.get(0));
				}				
			}	
		}
		
		return SUCCESS;
	}
	
	public String stockIn() throws Exception {
		
		return SUCCESS;
	}
	public String queryOrder() throws Exception{
		order = orderInfoDAO.selectByPrimaryKey(orderid);
		errorMessage = "";
		if (null == order) {
			errorMessage = "norecord";
		}else{
			ActionContext context = ActionContext.getContext();
			Map session = context.getSession();
			Admininfo admin = (Admininfo) session.get("admin");
			if (null != admin) {			
				if (session.containsKey("warehouse")) {
					warehouseinfo = (Warehouseinfo) session.get("warehouse");
				}else{
					WarehouseinfoExample example = new WarehouseinfoExample();
					Criteria criteria = example.createCriteria();
					criteria.andWhkeeperidEqualTo(admin.getId());
					warehouseList = warehouseinfoMapper.selectByExample(example);
					warehouseinfo = warehouseList.get(0);
				}
				if(null != orderid && !"".equals(orderid)){
					InventoryinfoExample example = new InventoryinfoExample();
					com.lkwlw.tplms.repertory.dao.model.InventoryinfoExample.Criteria criteria =
							example.createCriteria();
					criteria.andOrderidEqualTo(orderid);
					criteria.andWarehouseidEqualTo(warehouseinfo.getId());
					criteria.andStockinoutEqualTo(1);
					List<Inventoryinfo> stockInList = inventoryinfoMapper.selectByExample(example);
					if (null != stockInList && 0 < stockInList.size()) {
						errorMessage = "duplicate_stockin";	
						inventoryinfo = inventoryinfoMapper.selectByExample(example).get(0);
					}else {
						inventoryinfo.setId(null);
						inventoryinfo.setOrderid(orderid);
						inventoryinfo.setStockinout(1);
						inventoryinfo.setWarehouseid(warehouseinfo.getId());
						inventoryinfo.setRegistrant(admin.getFullName());
						inventoryinfo.setRemark("订单已扫描进入" + warehouseinfo.getWarehouse() + "仓库");
						inventoryinfoMapper.insertSelective(inventoryinfo);
						Orderinfo orderinfo =  new Orderinfo();
						orderinfo.setId(orderid);
						orderinfo.setState("已入库");
						orderInfoDAO.updateStateByPrimaryKey(orderinfo);						
						//example.setOrderByClause("registerDate desc");
						inventoryinfo = inventoryinfoMapper.selectByExample(example).get(0);
						errorMessage = "success";
					}
				}
			}			
		}
		execute();
		orderid = "";
		return SUCCESS;
	}
	public String cancelStockIn() throws Exception{
		errorMessage = "";
		execute();
		return SUCCESS;
	}
	public void setInventoryinfoMapper(InventoryinfoMapper inventoryinfoMapper) {
		this.inventoryinfoMapper = inventoryinfoMapper;
	}

	public void setWarehouseinfoMapper(WarehouseinfoMapper warehouseinfoMapper) {
		this.warehouseinfoMapper = warehouseinfoMapper;
	}
	
	public void setOrderInfoDAO(OrderinfoMapper orderInfoDAO) {
		this.orderInfoDAO = orderInfoDAO;
	}
	
	public void setInventoryinfo(Inventoryinfo inventoryinfo) {
		this.inventoryinfo = inventoryinfo;
	}
	public Inventoryinfo getInventoryinfo() {
		return inventoryinfo;
	}

	public Warehouseinfo getWarehouseinfo() {
		return warehouseinfo;
	}
	public void setWarehouseinfo(Warehouseinfo warehouseinfo) {
		this.warehouseinfo = warehouseinfo;
	}

	public List<Warehouseinfo> getWarehouseList() {
		return warehouseList;
	}

	public void setWarehouseList(List<Warehouseinfo> warehouseList) {
		this.warehouseList = warehouseList;
	}
	public List<Map<String, Object>> getStockInOrderList() {
		return stockInOrderList;
	}

	public void setStockInOrderList(List<Map<String, Object>> stockInOrderList) {
		this.stockInOrderList = stockInOrderList;
	}

	public String getOrderid() {
		return orderid;
	}

	public void setOrderid(String orderid) {
		this.orderid = orderid;
	}
	
	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}
	
	public void setOrder(Orderinfo order) {
		this.order = order;
	}
	 public Orderinfo getOrder(){
		 return order;
	 }
}
